//
//  RootHeaderFooterView.m
//  DemoProject
//
//  Created by hao on 2018/7/18.
//  Copyright © 2018年 hao. All rights reserved.
//

#import "RootHeaderView.h"
#import "SettingGroupModel.h"

@interface RootHeaderView ()

@property (nonatomic, weak) UIButton *btn;

@end

@implementation RootHeaderView

//在init方法中设置frame是没用的
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        //创建头部所需的控件
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        //设置按钮的尖尖图片
        [btn setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];
        //设置按钮的背景图片
        [btn setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted"] forState:UIControlStateHighlighted];
        //设置图片内容模式,不填充整个imageView
        btn.imageView.contentMode = UIViewContentModeCenter;
        //        btn.imageView.clipsToBounds = NO;//设置超出部分不剪切
        btn.imageView.layer.masksToBounds = NO;//效果等同于btn.imageView.clipsToBounds = NO;
        
        //1.设置内容左对齐
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        //2.设置按钮的内边距，按钮的内容距离左边有一顶距离
        btn.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
        //3.设置按钮的标题和图片之间的距离
        btn.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
        //设置标题文本的颜色
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //设置按钮文本的颜色
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        //设置按钮的监听
        [btn addTarget:self action:@selector(clickExpandListCollapse:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        self.btn = btn;
    }
    return self;
}

//该方法在控件的frame被改变的时候掉用
//该方法一般用于调整控件的位置
- (void)layoutSubviews
{
    // 一定要掉用父类的layoutSubviews
    [super layoutSubviews];
    //1.设置button的frame
    self.btn.frame = self.bounds;
}

- (void)setGroupModel:(SettingGroupModel *)groupModel
{
    _groupModel = groupModel;
    //设置按钮的文本
    [self.btn setTitle:groupModel.groupHeadTitle forState:UIControlStateNormal];
    //设置label的文本
//    self.label.text = [NSString stringWithFormat:@"%d/%ld", groupModel.online, groupModel.friends.count];
}

#pragma mark - 按钮监听方法，展开合拢列表
- (void)clickExpandListCollapse: (UIButton *) btn
{
    NSLog(@"clickExpandListCollapse");
    self.groupModel.open = !self.groupModel.isOpen;
    // 由于复用问题导致会多个headview的btn会旋转 (如果被SettingGroupModel对象持有就不会)
    self.btn.imageView.transform = self.groupModel.isOpen ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformIdentity;
    //如果代理对象实现了方法才执行
    if ([self.delegate respondsToSelector:@selector(headViewLoadData:)]) {
        [self.delegate headViewLoadData:self];
    }else{
        NSLog(@"未实现代理");
    }
}

#pragma mark - 当一个控件被添加到其它视图上的时候会掉用以下方法
//控件即将被添加到父视图上调用
- (void)willMoveToSuperview:(UIView *)newSuperview
{
//    NSLog(@"willMoveToSuperview");
//    self.btn.imageView.transform = self.groupModel.isOpen ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformIdentity;
}

//控件已经被添加到父视图上调用
- (void)didMoveToSuperview
{
    //    NSLog(@"didMoveToSuperview");
}

@end
